<template style="overflow-y: scroll; overflow-x: hidden; height: 100%">
  <el-drawer
    :title="label + '表格'"
    size="37%"
    :visible.sync="dialog"
    custom-class="demo-drawer"
    @close="handleClose"
  >
    <div class="demo-drawer__content">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="90px">
<!--        <el-form-item label="记录类型" prop="type">-->
<!--          <el-radio-group disabled @change="typeChange" v-model="temp.type">-->
<!--            <el-radio :label="0">自定义表格</el-radio>-->
<!--            <el-radio :label="1">文件</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
        <el-form-item label="选择产品" prop="productValue">
          <el-cascader
            ref="cascader"
            v-model="temp.productValue"
            :options="productDictCodeOptions"
            @change="changeProductDictCode"
          ></el-cascader>
        </el-form-item>
<!--        <el-form-item v-if="temp.productDictCode" label="产品子项" prop="subProductDictCode">-->
<!--          <el-select v-model="temp.subProductDictCode" placeholder="请选择">-->
<!--            <el-option-->
<!--              v-for="item in subProductDictCodeOptions"-->
<!--              :key="item.tag"-->
<!--              :label="item.label"-->
<!--              :value="item.value">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item v-if="temp.type == 1" label="选择文件">-->
<!--          <el-cascader-->
<!--            v-model="fileLevel"-->
<!--            disabled-->
<!--            @change="changeType"-->
<!--            :options="documentCategory" :show-all-levels="false">-->
<!--          </el-cascader>-->
<!--        </el-form-item>-->
        <el-form-item label="选择文件" prop="documentId">
          <el-select v-if="documentList && documentList.length > 0" v-model="temp.documentId" filterable placeholder="请选择">
            <el-option
              v-for="(item, index) in documentList"
              :key="item.value"
              :label="item.documentName"
              :value="item.documentId">
            </el-option>
          </el-select>
          <span v-else>暂无文件信息</span>
        </el-form-item>
        <el-form-item v-if="fileLevel && fileLevel != ''" label="选择知识库" prop="repositoryId">
          <el-cascader
            v-model="repository"
            @change="repositoryChange"
            :props="optionProps"
            :options="repositoryList" :show-all-levels="false">
          </el-cascader>
        </el-form-item>
        <el-form-item label="表格名称" prop="dataTemplateName">
          <el-input v-model.trim="temp.dataTemplateName" size="small" />
        </el-form-item>
      </el-form>
      <div style="text-align: right;padding-right: 20px" class="demo-drawer__footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button v-if="this.temp.manufactureRecordTemplateId" type="primary" @click="handleUpdate"> 修 改 </el-button>
        <el-button v-else type="primary" @click="handleCommit"> 确 定 </el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
import { saveManufactureRecordTemplate, updateManufactureRecordTemplate } from "@/api/rs/manufactureRecordTemplate"
import {listDictTreeItem} from "@/api/base/dict";
import {getDocumentListByType} from "@/api/xd/documentFlow";
import {listTeamTree} from "@/api/fd/repository";
import permissionList from "@/utils/permissionList";
export default {
  props: ['dialogVisible','productDictCodeOptions'],
  data() {
    return {
      optionProps: {
        value: 'id',
        label: 'name',
        children: 'children'
      },
      subProductDictCodeOptions: [],
      label: '',
      dialog: false,
      temp: {
        type: 1,
        productDictCode: null,
        subProductDictCode: null,
        fileLevel: "3,JL",
        documentId: "",
        repositoryId: "",
        dataTemplateName: "",
        productValue: []
      },
      rules: {
        type: [{required: true, message: "内容不可为空", trigger: "change"}],//产品项目父
        productDictCode: [{required: true, message: "内容不可为空", trigger: "change"}],//产品项目父
        subProductDictCode: [{required: true, message: "项目不可为空", trigger: "change"}],//产品项目子
        productValue: [{required: true, message: "产品不可为空", trigger: "change"}],//产品项目子
        dataTemplateName: [{required: true, message: "模板名称", trigger: "change"}],//表格名称
        repositoryId: [{required: true, message: "知识库不可为空", trigger: "change"}],//表格名称
        documentId: [{required: true, message: "文件不可为空", trigger: "change"}],//表格名称
      },
      fileLevel: ['3','JL'],
      documentCategory: [],
      documentList: [],
      repositoryList: [],
      repository: [],
      productValue: []
    }
  },
  watch: {
    dialogVisible(val) {
      console.log(val,'s22222222');
      this.dialog = val.dialogVisible
      this.label = val.label
      if (val.updateData != null) {
        let data = val.updateData
        console.log(JSON.stringify(data), 'data123')
        this.temp.productDictCode = data.productDictCode
        this.temp.subProductDictCode = data.subProductDictCode
        console.log(this.temp, 'this.temp')
        this.temp.productValue = [this.temp.productDictCode, this.temp.subProductDictCode]
        this.$forceUpdate()
        this.fileLevel = data.fileLevel.split(",")
        this.changeType(this.fileLevel)
        this.getRepository(data.repositoryId)
        this.temp = data
      }
    }
  },
  mounted() {
    this.dialogVisible
    this.getDocumentCategory()
    this.getRepositoryList()
    this.changeType(this.fileLevel)
    console.log(this.dialogVisibleCreate,this.productDictCodeOptions,'productDictCodeOptions');
  },
  methods: {
    getRepository(id){
      console.log(id, 'id')
      console.log(this.repositoryList, 'this.repositoryList')
      this.repositoryList.forEach(e => {
        let list = e.children
        if (list) {
          for (let i = 0; i < list.length; i++) {
            let data = list[i]
            if (id == data.id) {
              this.repository = [e.id,data.id]
            }
          }
        }
      })
      console.log(this.repository, 'this.repository')
    },
    repositoryChange(val){
      console.log(val, 'val')
      this.temp.repositoryId = val[1]
      console.log(this.temp.repositoryId, 'this.temp.repositoryId')
    },
    getRepositoryList(){
      listTeamTree({personal: false, permission: permissionList.VISIBLE}).then(res => {
        this.repositoryList = res.data
      })
    },
    typeChange(val){
      if (val == 1) {
        this.rules.documentId = [{required: true, message: "4文件不可为空", trigger: "change"}]//文档id
      }else {
        this.temp.fileLevel = null
        this.fileLevel = []
        this.temp.documentId = ""
      }
    },
    changeType(val){
      this.temp.documentId = ""
      let documentType = val[val.length - 1]
      this.temp.fileLevel = this.fileLevel[0] + ',' + this.fileLevel[1]
      getDocumentListByType({ type: documentType}).then(res => {
        this.documentList = res.data
      })
    },
    getDocumentCategory(){
      listDictTreeItem({
        dictGroupCode: 'document_category',
        parentDictId: 0
      }).then(res => {
        this.documentCategory = res.data
      })
    },
    handleUpdate(){
      console.log(this.temp, 'this.temp')
      this.$refs.dataForm.validate(vali => {
        if (vali) {
          updateManufactureRecordTemplate(this.temp).then(res => {
            this.$message.success("修改模板成功")
            this.dialog = false
            this.$emit('getListData',true)
          })
        }
      })
    },
    handleClose(){
      console.log("关闭时调用")
      this.temp = {
        type: 1,
        productDictCode: null,
        subProductDictCode: null,
        fileLevel: "3,JL",
        documentId: "",
        repositoryId: "",
        dataTemplateName: null
      }
      this.$refs.dataForm.resetFields()
      this.$refs.dataForm.clearValidate()
    },
    changeProductDictCode(val){//当一级项目发生改变时
      console.log(val, 'valvalvalva')
      this.temp.productDictCode = val[0]
      this.temp.subProductDictCode = val[1]
    },
    handleCancel() {
      this.dialog = false
    },
    handleCommit() {
      console.log(this.temp, 'this.temp')
      this.$refs.dataForm.validate(vali => {
        if (vali) {
          saveManufactureRecordTemplate(this.temp).then(res => {
            this.$message.success("添加模板成功")
            this.dialog = false
            this.$emit('getListData',true)
            // this.$router.push('/manufacture/data_template_design/' + res.data.manufactureRecordTemplateId)
          })
        }
      })
    }
  }
}
</script>
